package com.changpay.zywy.ui.activity.mine

import android.content.DialogInterface
import android.content.Intent
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.Preference
import kale.ui.view.dialog.EasyDialog
import kotlinx.android.synthetic.main.activity_bind_bank_card.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap

class BindBankCardActivity : BaseActivity(), IView {

    private lateinit var area_name: String
    private lateinit var area_province: String
    /**
     * local username
     */
    private var name: String by Preference(Constant.TRUE_NAME, "")
    private var card_number: String by Preference(Constant.CARD_NUMBER, "")

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> {
                val builder = EasyDialog.builder(this) // 建立builder对象
                builder.setTitle("提示")
                    .setMessage("是否放弃绑定银行卡")
                    .setOnCancelListener { dialogInterface ->

                    }
                    .setOnDismissListener(DialogInterface.OnDismissListener {  })
                    // 设置下方的三个按钮
                    .setPositiveButton("确定") { dialog, which -> (App.instance as App).finishTopActivity(2) }
                    .setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                    .setCancelable(true) // 点击空白处可以关闭

                val easyDialog = builder.build<EasyDialog>()

                // 用showAllowingStateLoss()弹出
                easyDialog.show(supportFragmentManager, "")
            }
            R.id.bind_submit -> sumbit()
            R.id.tv_get_code -> getCode()
            R.id.tv_city ->  { Intent(this, LocationSelectActivity::class.java).run {
                    startActivityForResult(this, 100)
                } }
        }
    }

    private fun sumbit() {
        val account = bind_name.getInputText()
        if (TextUtils.isEmpty(account)) {
            showToast("请输入持卡人姓名")
            return
        }
        val CardNumber = bind_band_number.getInputText()
        if (TextUtils.isEmpty(CardNumber)) {
            showToast("请输入银行卡号")
            return
        }
        val Identity = bind_qr_number.getInputText()
        if (TextUtils.isEmpty(Identity)) {
            showToast("请输入身份证号")
            return
        }
        val city = tv_city.text.toString()
        if (TextUtils.isEmpty(city)) {
            showToast("请选择开户银行的城市")
            return
        }
        val phone = bind_phone_number.getInputText()
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入预留手机")
            return
        }

        val MobileCode = bind_code.getInputText()
        if (TextUtils.isEmpty(MobileCode)) {
            showToast("请输入验证码")
            return
        }
        val params = HashMap<String, String>()
        params["key"] = token
        params["bank_card"] = CardNumber
        params["city"] = area_name
        params["key"] = token
        params["user_name"] = account
        params["idcard"] = Identity
        params["phone"] = phone
        params["captcha"] = MobileCode

        RetrofitHelper.service.bank_card_add(params).sss(this)
        {
            showToast(it.result.toString())
            (App.instance as App).finishTopActivity(2)
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_bind_bank_card
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = "绑定银行卡"
        bind_qr_number.setInputText(card_number)
        bind_name.setInputText(name)
        bind_code.setCodeOnclickListe(onClickListener)
        iv_back.setOnClickListener(onClickListener)
        bind_submit.setOnClickListener(onClickListener)
        tv_city.setOnClickListener(onClickListener)
    }

    override fun start() {

    }


    /**
     * 获取验证码
     */
    private fun getCode() {
        val phone = bind_phone_number.getInputText()
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号码")
            return
        }
        val param = hashMapOf<String, String>()
        param["phone"] = phone
        param["type"] = "5"
        RetrofitHelper.service.get_sms_captcha(param).sss(this) {
            showToast("发送验证码成功")
            bind_code.starCode()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val builder = EasyDialog.builder(this) // 建立builder对象
            builder.setTitle("提示")
                .setMessage("是否放弃绑定银行卡")
                .setOnCancelListener { dialogInterface ->

                }
                .setOnDismissListener { dialogInterface ->

                }
                // 设置下方的三个按钮
                .setPositiveButton("确定") { dialog, which -> (App.instance as App).finishTopActivity(2) }
                .setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                .setCancelable(true) // 点击空白处可以关闭

            val easyDialog = builder.build<EasyDialog>()

            // 用showAllowingStateLoss()弹出
            easyDialog.show(supportFragmentManager, "")
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            tv_city.text = data?.getStringExtra("area_province").toString() + " " +
                    data?.getStringExtra("area_name").toString()
            area_province = data?.getStringExtra("area_province").toString()
            area_name = data?.getStringExtra("area_name").toString()
        }
    }
}

